-- 样式设置表
Styles = {}

Style = Style or Class("Style")

---@diagnostic disable-next-line: duplicate-set-field
function Style:ctor(args, superStyle)
    self.name = args.name or "style"
    self.color = args.color or (superStyle and superStyle.color) or Color.white
    if args.hover then
        self.hover = Style.new(args.hover, self)
    end
    if args.selected then
        self.selected = Style.new(args.selected, self)
    end
    if args.forbidden then
        self.forbidden = Style.new(args.forbidden, self)
    end
end

Styles.default = Style.new({name="default"})
Styles.label = Style.new({
    name = "label",
    color = Color.black,
})
Styles.button = Style.new({
    name = "button",
    color = Color.white,
})

Styles.buttonLabel = Style.new({
    name = "buttonLabel",
    color = Color.black,
    hover = {
        color = Color.fromHex("#56A4F8")
    },
    selected = {
        color = Color.fromHex("#56A4F8")
    },
    forbidden = {
        color = Color.fromHex("#4E4E4E")
    }
})

-- 一个普通的hover style
Styles.hover = Style.new{
    color = Color.zero,
    hover = {
        color = Color.lightBlue
    }
}

Styles.textEditorBorder = Style.new{
    color = Color.zero,
    hover = {
        color = Color.black
    }
}

StyleEvent = RegisterEvents{
    "StateChanged",
    "FocusChanged",
    "FocusIn",
    "FocusOut",
    "StyleChanged",
    "SubStyleChanged"
}
local function syncSubStyle(self, subStyle)
    self.subStyle = subStyle
end
local function bindTarget(self, target)
    target:on(StyleEvent.SubStyleChanged, syncSubStyle, self)
end

local function unbindTarget(self, target)
    target:off(StyleEvent.SubStyleChanged, syncSubStyle, self)
end

local function applyStyle(self, style)
    if self.subStyle and style[self.subStyle] then
        applyStyle(self, style[self.subStyle])
        return
    end
    -- 没有渲染组件的对象赋值了color后，会导致后续使用color属性时无法正确赋值
    -- 考虑将节点状态，和样式表现分开
    if self:hasCompoent(Renderer) then
        self.color = style.color
    end
end

Styled = {
    props = {
        _subStyle = "none"
    },
    gets = {
        subStyle = function (self)
            return self._subStyle
        end,
        styleTarget = function(self)
            return self._styleTarget
        end,
        style = function (self)
            return self._style
        end
    },
    sets = {
        subStyle = function (self, key, val)
            self._subStyle = val
            applyStyle(self, self.style)
            self:emit(StyleEvent.SubStyleChanged, val)
        end,
        style = function (self, key, val)
            self._style = val
            self:emit(StyleEvent.StyleChanged, val)
            applyStyle(self, val)
        end,
        styleTarget = function (self, key, val)
            if self._styleTarget then
                unbindTarget(self, self._styleTarget)
            end
            self._styleTarget = val;
            bindTarget(self, val)
        end,
    },
    dependencies = {
        EventEmitter
    }
}

function Styled:ctor()
    self._style = Styles.default
end